const app = getApp();
import { jiatuiRequest } from "/utils/requests";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showContact: true,
    suspendShow: true,
    showIcon: true,
    bgcolor: '',
    options: {
      s1: 'y',
      xiangmu: "sk",
      lx: "sk",
      selectNo: 'y',
    },
    yl: 1,
    lx: 'sk',
    myPhone: '',
    cxlists: [],
    chaxunjg: 0,
    options: [],
    page: 1,
    zcpages: 1,
    onReachBottomNum: 0,
    grdata: [], //查询个人数据
    qxdata: [] //取消时的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData(await app.methods.myPhone()); // 获取本地存储手机号
    if (options.data != undefined) {
      options = JSON.parse(options.data);
    } else {
      options = options;
    }
    if (options.yl != undefined) {
      if (options.yl.length > 0) {
        this.setData({
          yl: Number(options.yl)
        })
      }
    }
    this.setData({
      options,
      onReachBottomNum: 0,
    })
    let grdata = {
      phone: this.data.myPhone,
      page: 1,
      limits: 20
    }
    this.setData({
      grdata: grdata
    })
    let obj = await this.getCollectPositions(grdata);
    if (obj.total != 0) {
      this.setData({
        cxlists: this.data.cxlists.concat(obj.result),
        chaxunjg: obj.total,
      })
    }
    console.log(this.data)
    // this.setData(this.getUser(grdata))
  },
  // 收藏/取消收藏
  collect: async function (e) {
    if (this.data.myPhone != '') {
      let {
        ggid,
        zwid,
        index,
        action
      } = e.currentTarget.dataset;
      xhs.showLoading({
        title: '取消收藏中！',
        icon: 'none',
        duration: 2000,
      })
      let {
        cxlists,
        chaxunjg
      } = this.data;
      let data = {
        ggid,
        zwid,
        action,
        phone: this.data.myPhone,
      }
      let collectData = await this.updateCollect(data)
      console.log(collectData)
      xhs.hideLoading({})
      xhs.showToast({
        title: collectData == 1 ? '取消收藏成功!' : '取消收藏失败!',
        icon: 'success',
        duration: 1500
      })
      // 删除该条数据
      cxlists.splice(index, 1);
      chaxunjg -= 1;
      this.setData({
        cxlists,
        chaxunjg,
      })
    } else {
      xhs.showToast({
        title: '请先注册',
        duration: 2000
      })
      if (app.globalData.PrivacyAuthorization) {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !0,
        })
      } else {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !1,
        })
      }
      return false
    }
  },

  // 收藏和取消的方法
  updateCollect(searchData) {
    return new Promise((resolve) => {
      jiatuiRequest({
        url: `/api/v1/moonshot/collectPosition`,
        method: 'POST',
        dataType: 'jsonp',
        header: {
          'X-Requested-From': 'WeChatMiniProgram'
        },
        data: {
          ...searchData
        },
      }).then(res => {
        console.log(res)
        let { code, data } = res;
        if (code == 0) {
          resolve(1)
        } else {
          resolve(0)
        }
      }).catch(err => {
        console.log(err)
        resolve(0)
      })
    })
  },

  // 获取收藏的职位
  async getCollectPositions(searchData) {
    return new Promise((resolve) => {
      jiatuiRequest({
        url: `/api/v1/moonshot/getCollectPositions`,
        method: 'GET',
        dataType: 'jsonp',
        header: {
          'X-Requested-From': 'WeChatMiniProgram'
        },
        data: {
          ...searchData
        },
      }).then(res => {
        console.log(res)
        let { code, data } = res;
        if (code == 0 && data && data.result && data.result.length > 0) {
          resolve(data)
        } else {
          resolve({
            result: [],
            total: 0
          })
        }
      }).catch(err =>{
        console.log(err)
        resolve({
          result: [],
          total: 0
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 回到顶部
  toTop() {
    xhs.pageScrollTo({
      scrollTop: 0
    })
  },
  //去往查询
  goToCX: function () {
    var options = {
      lx: this.data.options.lx,
      actid: this.data.options.actid,
      xiangmu: this.data.options.xiangmu
    }
    xhs.navigateBack({})
  },
  // 展开和收起
  toggle: function (e) {
    const {
      index
    } = e.currentTarget.dataset;
    // console.log(index)
    let {
      cxlists
    } = this.data;
    cxlists[index].toggle = !cxlists[index].toggle
    this.setData({
      cxlists
    })
  },
  // 公告查看
  gonggao: function (e) {
    const { notice_url } = e.currentTarget.dataset;
    xhs.navigateTo({
      url: `/packageB/gg/gg?link=${notice_url}`,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let { fx = 'hbwtb', gh = 'zcj37368', id } = this.data.options;
    return {
      title: '中公AI职位库',
      path: `/packageB/zwk/index?id=${id}&fx=${fx}&gh=${gh}`,
      imageUrl: 'http://news01.offcn.com/hb/2025/0205/20250205025923173.jpg',
    }
  }
})